﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Function 的摘要说明
/// </summary>
public class Function
{

    ZSql zs = new ZSql();
    public Function()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    //获取表中最大值
    public string getMaxId(string table, string id)
    {
        string getMaxId;
        zs.Open("select top 1 " + id + " from " + table + " order by " + id + " desc").ToString();
        if (zs.m_table.Rows.Count > 0)
            getMaxId = zs.m_table.Rows[0][id].ToString();
        else
            getMaxId = "0";

        zs.Close();
        return getMaxId;

    }

    //获取表中库存(id:条码)
    public string getStock(string id, string StockType)
    {
        string getStock;
        int ProcNum = 0;
        int number = 0;
        zs.Open("SELECT ISNULL(SUM(ProcNum), 0) AS ProcNum FROM CS_Stock WHERE ComodBarcode = '"+id+"'").ToString();
        if (zs.m_table.Rows.Count > 0)
            ProcNum = Convert.ToInt32(zs.m_table.Rows[0]["ProcNum"]);
        zs.Close();

        zs.Open("SELECT ISNULL(SUM(number), 0) AS number FROM CS_Sales WHERE comodBarcode = '" + id + "' and isDel=0").ToString();
        if (zs.m_table.Rows.Count > 0)
            number = Convert.ToInt32(zs.m_table.Rows[0]["number"]) ;
        zs.Close();
        getStock = Convert.ToString(ProcNum - number);
        return getStock;

    }

    //获取表中售后库存(id:条码)
    public string getSVStock(string id)
    {
        string getSVStock;
        double ProcNum = 0;
        double number = 0;
        zs.Open("SELECT ISNULL(SUM(ProcNum), 0) AS ProcNum FROM SV_Stock WHERE ComodBarcode = '" + id + "'").ToString();
        if (zs.m_table.Rows.Count > 0)
            ProcNum = Convert.ToDouble(zs.m_table.Rows[0]["ProcNum"]);
        zs.Close();

        zs.Open("SELECT ISNULL(SUM(OutNum), 0) AS number FROM SV_OutStock WHERE comodBarcode = '" + id + "'").ToString();
        if (zs.m_table.Rows.Count > 0)
            number = Convert.ToDouble(zs.m_table.Rows[0]["number"]);
        zs.Close();
        getSVStock = Convert.ToString(ProcNum - number);
        return getSVStock;

    }



    //字符截取
    //text:文本
    //length 长度(汉字、中文符号算字节,英文符号算一个字节）
    public string getText(string text, int length)
    {
        if (length < text.Length)
            return text.Substring(0, length) + "..";
        else
            return text;
    }

    //四舍五入转整型
    //source:数字
    public string RoundInt(string source) //四舍五入转整型
    {
        int start = source.IndexOf('.');
        if (start > -1)//小数
        {
            int intnum = Convert.ToInt32(source.Substring(0, start));//整型数字
            int intminnum = Convert.ToInt32(source.Substring(start + 1, 1));//小数点第一位
            if (intminnum > 4)//四舍五入
            {
                intnum++;
            }
            source = intnum.ToString();
        }
        return source;
    } 


    //通过栏目编号获取栏目所在位置
    //---------------栏目位置----------------------------
    public string getModuleidAdd(string moduleid)
    {
        ZSql menu = new ZSql();
        string strpath = "";
        menu.Open("select a.menu ,(select c.menu from menu c where c.menuid=a.parentid and c.menunode=0 and c.parentid<>0) as menu1,(select e.menu from menu e where e.menuid=(select d.parentid from menu d where d.menuid=(select b.menuid from menu b where b.menuid=a.parentid ))) as menu2 from menu a where a.menunode=1 and a.menuid=" + moduleid);
        if (string.IsNullOrEmpty(menu.m_table.Rows[0]["menu1"].ToString()) && string.IsNullOrEmpty(menu.m_table.Rows[0]["menu2"].ToString()))
        {
            zs.Open("select a.menu ,(select c.menu from menu c where c.menuid=a.parentid ) as menu1 from menu a where a.menunode=1 and a.menuid=" + moduleid);

            strpath = zs.m_table.Rows[0]["menu1"] + " >> " + zs.m_table.Rows[0]["menu"];
        }
        else
        {
            strpath = menu.m_table.Rows[0]["menu2"] + " >> " + menu.m_table.Rows[0]["menu1"] + " >> " + menu.m_table.Rows[0]["menu"];
        }

        zs.Close();
        menu.Close();
        return strpath;




    }

    //字符截取
    //text:文本
    //length 长度(汉字、中文符号算字节,英文符号算一个字节）
    public string CutStr(string sInString, int iCutLength)
    {
        if (sInString == null || sInString.Length == 0 || iCutLength <= 0)
            return "";
        int iCount = System.Text.Encoding.GetEncoding("Shift_JIS").GetByteCount(sInString);
        if (iCount > iCutLength)
        {
            int iLength = 0;
            for (int i = 0; i < sInString.Length; i++)
            {
                int iCharLength = System.Text.Encoding.GetEncoding("Shift_JIS").GetByteCount(new char[] { sInString[i] });
                iLength += iCharLength;
                if (iLength == iCutLength)
                {
                    sInString = sInString.Substring(0, i + 1);
                    break;
                }
                else if (iLength > iCutLength)
                {
                    sInString = sInString.Substring(0, i);
                    break;
                }
            }
        }
        return sInString;
    }

//-------------------------------------------------------
//取字段值(目的表名,目的字段名,条件)------------
    public string GetTableValue(string TableName, string Field,string where)
    {
        string FieldValue = "";
	    if(TableName!="" && Field!="")
        {
		    string strSql = "Select top 1 "+ Field +" from "+ TableName +" where 1=1 and "+ where;
		    zs.Open(strSql);
            if (zs.m_table.Rows.Count > 0)
            {
                FieldValue = zs.m_table.Rows[0][Field].ToString();
            }
		    zs.Close();
         }
         return FieldValue;
    }

    //获取操作权限(userId操作人,moduleid栏目,scell模板列,gvname模板名,删除按钮btnamedel,审核按钮btnamedel,增加按钮btnAdd)

    public void checkLevels(string userId, string moduleid, int scell, GridView gvname, Button btnamedel, Button btnameflge, Button btnAdd)
    {
        ZSql zsql = new ZSql();
        string getLevels = "0";

        zsql.Open("select * from ModulePurview where ModuleId=" + moduleid + " and roleid=" + userId + " ");


        if (zsql.m_table.Rows.Count > 0)
        {

            getLevels = zsql.m_table.Rows[0]["Levels"].ToString();
            zsql.Close();
            for (int i = 0; i < gvname.Rows.Count; i++)
            {
                switch (getLevels)
                {
                    case "1":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("lnkbtnEdit") as LinkButton).Enabled = false;
                        (gvname.Rows[i].Cells[scell - 1].FindControl("lnkbtnFlag") as LinkButton).Enabled = false;
                        btnamedel.Enabled = false;
                        btnameflge.Enabled = false;
                        break;
                    case "2":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("lnkbtnEdit") as LinkButton).Enabled = false;
                        (gvname.Rows[i].Cells[scell - 1].FindControl("lnkbtnFlag") as LinkButton).Enabled = false;

                        btnameflge.Enabled = false;
                        btnAdd.Enabled = false;
                        break;
                    case "4":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("lnkbtnFlag") as LinkButton).Enabled = false;
                        btnAdd.Enabled = false;
                        btnamedel.Enabled = false;
                        btnameflge.Enabled = false;
                        break;
                    case "8":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("lnkbtnEdit") as LinkButton).Enabled = false;
                        btnAdd.Enabled = false;
                        btnamedel.Enabled = false;
                        break;
                    case "3":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("lnkbtnEdit") as LinkButton).Enabled = false;
                        (gvname.Rows[i].Cells[scell - 1].FindControl("lnkbtnFlag") as LinkButton).Enabled = false;
                        btnameflge.Enabled = false;
                        break;
                    case "5":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("lnkbtnFlag") as LinkButton).Enabled = false;
                        btnamedel.Enabled = false;
                        btnameflge.Enabled = false;
                        break;
                    case "9":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("lnkbtnEdit") as LinkButton).Enabled = false;
                        btnamedel.Enabled = false;
                        break;
                    case "6":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("lnkbtnFlag") as LinkButton).Enabled = false;
                        btnAdd.Enabled = false;
                        btnameflge.Enabled = false;
                        break;
                    case "10":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("lnkbtnEdit") as LinkButton).Enabled = false;
                        btnAdd.Enabled = false;
                        break;
                    case "12":
                        btnAdd.Enabled = false;
                        btnamedel.Enabled = false;
                        break;
                    case "0":
                        btnAdd.Enabled = false;
                        btnamedel.Enabled = false;
                        btnameflge.Enabled = false;
                        (gvname.Rows[i].Cells[scell - 1].FindControl("lnkbtnEdit") as LinkButton).Enabled = false;
                        (gvname.Rows[i].Cells[scell - 1].FindControl("lnkbtnFlag") as LinkButton).Enabled = false;
                        break;
                    case "15":
                        break;

                }
            }
        }
        else
        {
            for (int j = 0; j < gvname.Rows.Count; j++)
            {

                (gvname.Rows[j].Cells[scell - 1].FindControl("lnkbtnEdit") as LinkButton).Enabled = false;
                (gvname.Rows[j].Cells[scell - 1].FindControl("lnkbtnFlag") as LinkButton).Enabled = false;

            }
            btnAdd.Enabled = false;
            btnamedel.Enabled = false;
            btnameflge.Enabled = false;
        }

    }


    //2
    public void checkLevels(string userId, string moduleid, int scell, GridView gvname, Button btnamedel)
    {
        ZSql zsql = new ZSql();

        string getLevels = "0";


        zsql.Open("select * from ModulePurview where ModuleId=" + moduleid + " and roleid=" + userId + " ");

        if (zsql.m_table.Rows.Count > 0)
        {
            getLevels = zsql.m_table.Rows[0]["Levels"].ToString();
            zsql.Close();
            for (int i = 0; i < gvname.Rows.Count; i++)
            {
                switch (getLevels)
                {

                    case "1":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("lnkbtnEdit") as LinkButton).Enabled = false;
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnFlag") as LinkButton).Enabled = false;
                        btnamedel.Enabled = false;

                        break;
                    case "2":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("lnkbtnEdit") as LinkButton).Enabled = false;
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnFlag") as LinkButton).Enabled = false;



                        break;
                    case "4":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnFlag") as LinkButton).Enabled = false;

                        btnamedel.Enabled = false;

                        break;
                    case "8":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("lnkbtnEdit") as LinkButton).Enabled = false;

                        break;
                    case "3":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("lnkbtnEdit") as LinkButton).Enabled = false;
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnFlag") as LinkButton).Enabled = false;

                        break;
                    case "5":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnFlag") as LinkButton).Enabled = false;
                        btnamedel.Enabled = false;

                        break;
                    case "9":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("lnkbtnEdit") as LinkButton).Enabled = false;
                        btnamedel.Enabled = false;
                        break;
                    case "6":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnFlag") as LinkButton).Enabled = false;


                        break;
                    case "10":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("lnkbtnEdit") as LinkButton).Enabled = false;

                        break;
                    case "12":

                        btnamedel.Enabled = false;
                        break;
                    case "13":

                        btnamedel.Enabled = false;
                        break;
                    case "0":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnFlag") as LinkButton).Enabled = false;
                        btnamedel.Enabled = false;
                        break;
                    case "15":
                        break;


                }

            }

        }
        else
        {
            for (int j = 0; j < gvname.Rows.Count; j++)
            {

                // (gvname.Rows[j].Cells[scell - 1].FindControl("lnkbtnEdit") as LinkButton).Enabled = false;
                (gvname.Rows[j].Cells[scell - 1].FindControl("linkbtnFlag") as LinkButton).Enabled = false;

            }

            btnamedel.Enabled = false;

        }

    }

    //3
    public void checkLevels(string userId, string moduleid, int scell, GridView gvname, string a)
    {
        ZSql zsql = new ZSql();

        string getLevels = "0";


        zsql.Open("select * from ModulePurview where ModuleId=" + moduleid + " and roleid=" + userId + " ");

        if (zsql.m_table.Rows.Count > 0)
        {
            getLevels = zsql.m_table.Rows[0]["Levels"].ToString();
            zsql.Close();
            for (int i = 0; i < gvname.Rows.Count; i++)
            {
                switch (getLevels)
                {

                    case "1":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnPG") as LinkButton).Enabled = false;
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnFlag") as LinkButton).Enabled = false;




                        break;
                    case "2":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnPG") as LinkButton).Enabled = false;
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnFlag") as LinkButton).Enabled = false;




                        break;
                    case "4":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnFlag") as LinkButton).Enabled = false;


                        break;
                    case "8":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnSales") as LinkButton).Enabled = false;

                        break;
                    case "3":

                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnSales") as LinkButton).Enabled = false;
                        break;
                    case "5":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnFlag") as LinkButton).Enabled = false;


                        break;
                    case "9":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnPG") as LinkButton).Enabled = false;
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnSales") as LinkButton).Enabled = false;

                        break;
                    case "6":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnFlag") as LinkButton).Enabled = false;



                        break;
                    case "10":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnPG") as LinkButton).Enabled = false;


                        break;
                    case "0":

                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnPG") as LinkButton).Enabled = false;
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnFlag") as LinkButton).Enabled = false;

                        break;
                    case "15":


                        break;




                }

            }

        }
        else
        {
            for (int j = 0; j < gvname.Rows.Count; j++)
            {

                (gvname.Rows[j].Cells[scell - 1].FindControl("linkbtnPG") as LinkButton).Enabled = false;
                (gvname.Rows[j].Cells[scell - 1].FindControl("linkbtnFlag") as LinkButton).Enabled = false;



            }



        }

    }
    //4
    public void checkLevels(string userId, string moduleid, int scell, GridView gvname)
    {
        ZSql zsql = new ZSql();

        string getLevels = "0";


        zsql.Open("select * from ModulePurview where ModuleId=" + moduleid + " and roleid=" + userId + " ");

        if (zsql.m_table.Rows.Count > 0)
        {
            getLevels = zsql.m_table.Rows[0]["Levels"].ToString();
            zsql.Close();
            for (int i = 0; i < gvname.Rows.Count; i++)
            {
                switch (getLevels)
                {

                    case "1":

                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnSales") as LinkButton).Enabled = true;



                        break;
                    case "2":

                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnSales") as LinkButton).Enabled = true;




                        break;
                    case "4":

                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnSales") as LinkButton).Enabled = true;

                        break;
                    case "8":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnSales") as LinkButton).Enabled = true;

                        break;
                    case "3":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnSales") as LinkButton).Enabled = true;

                        break;
                    case "5":

                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnSales") as LinkButton).Enabled = true;

                        break;
                    case "9":


                        break;
                    case "6":

                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnSales") as LinkButton).Enabled = true;


                        break;
                    case "10":

                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnSales") as LinkButton).Enabled = true;

                        break;
                    case "12":

                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnSales") as LinkButton).Enabled = true;

                        break;
                    case "0":

                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnSales") as LinkButton).Enabled = true;

                        break;
                    case "15":
                        (gvname.Rows[i].Cells[scell - 1].FindControl("linkbtnSales") as LinkButton).Enabled = true;

                        break;
                }

            }

        }
        else
        {
            for (int j = 0; j < gvname.Rows.Count; j++)
            {


                (gvname.Rows[j].Cells[scell - 1].FindControl("linkbtnSales") as LinkButton).Enabled = false;

            }



        }

    }


   //5
    public void checkLevels(string userId, string moduleid, Button btnamedel)
    {
        ZSql zsql = new ZSql();

        string getLevels = "0";


        zsql.Open("select * from ModulePurview where ModuleId=" + moduleid + " and roleid=" + userId + " ");

        if (zsql.m_table.Rows.Count > 0)
        {
            getLevels = zsql.m_table.Rows[0]["Levels"].ToString();
            zsql.Close();
           
                switch (getLevels)
                {

                    case "1":
                        
                        btnamedel.Enabled = false;

                        break;
                    case "2":
                        
                        break;
                    case "4":
                        

                        btnamedel.Enabled = false;

                        break;
                    case "8":

                        btnamedel.Enabled = false;
                        break;
                    case "3":

                        btnamedel.Enabled = false;
                        break;
                    case "5":
                       
                        btnamedel.Enabled = false;

                        break;
                    case "9":
                       
                        btnamedel.Enabled = false;
                        break;
                    case "6":
                        


                        break;
                    case "10":
                       

                        break;
                    case "12":

                        btnamedel.Enabled = false;
                        break;
                    case "13":

                        btnamedel.Enabled = false;
                        break;
                    case "0":
                       
                        btnamedel.Enabled = false;
                        break;
                    case "15":
                        break;
 

            }

        }
        else
        {
          
            btnamedel.Enabled = false;

        }
        
        

    }
}

